package coint.btc.service.btcuser.impl;


import java.text.SimpleDateFormat;
import java.util.Date;

import javax.validation.ValidationException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

import coint.btc.dto.BtcUserDto;
import coint.btc.jpa.BtcUserJpa;
import coint.btc.pojo.BtcUser;
import coint.btc.service.btcuser.BtcUserService;
import coint.btc.vo.BtcUserVo;
/**
 * btc 用户信息Service实现
 * @author lt
 *
 */
@Service
public class BtcUserServiceImpl implements BtcUserService {
	
	@Autowired
    private BtcUserJpa userJpa;
	
	@Autowired
    private StringRedisTemplate stringRedisTemplate;
	
	@Override
	public BtcUserVo findUser(BtcUserDto userDto) {
		BtcUser userInfo= userJpa.findUser(userDto);
		BtcUserVo userVo=new BtcUserVo();
		BeanUtils.copyProperties(userInfo, userVo);
		return userVo;
	}

	@Override
	public void addUser(BtcUserDto userDto) {
		/*判断用户名/密码/身份证号不能重复*/
		Integer count=userJpa.checkIsNotRepeat(userDto);
		if(count>0) {
			throw new ValidationException("用户名/密码/身份证号不能重复");
		}
		/*默认为:待审核*/
		userDto.setTongguo(1);
		BtcUser user=new BtcUser();
		BeanUtils.copyProperties(userDto, user);
		userJpa.save(user);
		userDto.getRizhi().setShijian(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		userDto.getRizhi().setChenggong(1);
		stringRedisTemplate.convertAndSend("logChat",
				JSON.toJSONString(userDto.getRizhi()));
	}

	

}
